#include "Dispatch.h"
#include "dispatchluabindings.h"
#include "../core/LuaUtils.h"


void DispatchLuaBindings::pushStaticFuncs(lua_State* L)
{
	static const struct luaL_Reg mylib [] = {
		{"SendMessage", li_sendMessage},
		{"AddHandler", li_addHandler},
		{"AddNamedHandler", li_addNamedHandler},
		{"RemoveHandler", li_removeHandler},
		{NULL, NULL}  /* sentinel */
	};

	// dump them into said table
	luaL_register(L, NULL, mylib);
}

int DispatchLuaBindings::li_sendMessage(lua_State* L)
{
	Dispatch* op = (Dispatch*)typeCheckUnboxAO(L,1,"Dispatch");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	std::string a1 = getSafeLuaString(L, 2);
	IData a2 = popIData(L, 3);
	op->sendMessage(a1, a2);
	
	return 0;
}


int DispatchLuaBindings::li_addHandler(lua_State* L)
{
	Dispatch* op = (Dispatch*)typeCheckUnboxAO(L,1,"Dispatch");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	std::string a1 = getSafeLuaString(L, 2);
	AnyAOPtr a2 = unboxAOPtr(L, 3);
	op->addHandler(a1, a2);
	
	return 0;
}

int DispatchLuaBindings::li_addNamedHandler(lua_State* L)
{
	Dispatch* op = (Dispatch*)typeCheckUnboxAO(L,1,"Dispatch");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	std::string a1 = getSafeLuaString(L, 2);
	std::string a2 = getSafeLuaString(L, 3);
	AnyAOPtr a3 = unboxAOPtr(L, 4);
	op->addNamedHandler(a1, a2, a3);
	
	return 0;
}


int DispatchLuaBindings::li_removeHandler(lua_State* L)
{
	Dispatch* op = (Dispatch*)typeCheckUnboxAO(L,1,"Dispatch");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	std::string a1 = getSafeLuaString(L, 2);
	op->removeHandler(a1);
	
	return 0;
}


